class Solution {
public:
    int maxScore(vector<int>& cardPoints, int k) {
        int n = cardPoints.size();
        int sum = 0;
        
        for(int i = n - k;i < n;i++){
            sum += cardPoints[i];
        }
        
        int ans = sum;
        
        for(int i = 0;i < k;i++){
            sum -= cardPoints[i + n - k];
            sum += cardPoints[i];
            
            ans = max(sum,ans);
        }
        
        return ans;
    }
};
